package com.ruoyi.report_gc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_gc.domain.GjhjjtjlbBsDetail;
import com.ruoyi.report_gc.domain.vo.GjhjjtjlbBsDetailVo;
import com.ruoyi.report_gc.mapper.GjhjjtjlbBsDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_gc.domain.bo.GjhjjtjlbBsInfoBo;
import com.ruoyi.report_gc.domain.vo.GjhjjtjlbBsInfoVo;
import com.ruoyi.report_gc.domain.GjhjjtjlbBsInfo;
import com.ruoyi.report_gc.mapper.GjhjjtjlbBsInfoMapper;
import com.ruoyi.report_gc.service.IGjhjjtjlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 钢筋焊接接头试验检测记录Service业务层处理
 *
 * @author llb
 * @date 2024-06-24
 */
@RequiredArgsConstructor
@Service
public class GjhjjtjlbBsInfoServiceImpl implements IGjhjjtjlbBsInfoService {

    private final GjhjjtjlbBsInfoMapper baseMapper;
    private final GjhjjtjlbBsDetailMapper detailMapper;

    /**
     * 查询钢筋焊接接头试验检测记录
     */
    @Override
    @FIllBasicInfo
    public GjhjjtjlbBsInfoVo queryById(Long excelId){
        GjhjjtjlbBsInfoVo gjhjjtjlbBsInfoVo = baseMapper.selectVoById(excelId);
        List<GjhjjtjlbBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<GjhjjtjlbBsDetail>().eq(GjhjjtjlbBsDetail::getExcelId, excelId));
        gjhjjtjlbBsInfoVo.setDetailList(detailList);
        return gjhjjtjlbBsInfoVo;
    }

    /**
     * 查询钢筋焊接接头试验检测记录列表
     */
    @Override
    public TableDataInfo<GjhjjtjlbBsInfoVo> queryPageList(GjhjjtjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GjhjjtjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<GjhjjtjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钢筋焊接接头试验检测记录列表
     */
    @Override
    public List<GjhjjtjlbBsInfoVo> queryList(GjhjjtjlbBsInfoBo bo) {
        LambdaQueryWrapper<GjhjjtjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GjhjjtjlbBsInfo> buildQueryWrapper(GjhjjtjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GjhjjtjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, GjhjjtjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, GjhjjtjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), GjhjjtjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), GjhjjtjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), GjhjjtjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), GjhjjtjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), GjhjjtjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), GjhjjtjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), GjhjjtjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), GjhjjtjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), GjhjjtjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), GjhjjtjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), GjhjjtjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), GjhjjtjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), GjhjjtjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), GjhjjtjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), GjhjjtjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), GjhjjtjlbBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getJincrq()), GjhjjtjlbBsInfo::getJincrq, bo.getJincrq());
        lqw.eq(StringUtils.isNotBlank(bo.getPih()), GjhjjtjlbBsInfo::getPih, bo.getPih());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), GjhjjtjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), GjhjjtjlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), GjhjjtjlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), GjhjjtjlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), GjhjjtjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), GjhjjtjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), GjhjjtjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), GjhjjtjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增钢筋焊接接头试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(GjhjjtjlbBsInfoBo bo) {
        GjhjjtjlbBsInfo add = BeanUtil.toBean(bo, GjhjjtjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<GjhjjtjlbBsDetail> details = new ArrayList<>(6);
            for (int i=1;i<=6;i++ ){
                details.add(new GjhjjtjlbBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改钢筋焊接接头试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(GjhjjtjlbBsInfoBo bo) {
        GjhjjtjlbBsInfo update = BeanUtil.toBean(bo, GjhjjtjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), GjhjjtjlbBsDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GjhjjtjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除钢筋焊接接头试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
